package com.zb.sannuo.pharmacy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zb.sannuo.common.domain.Drugstore;
import com.zb.sannuo.common.domain.UserLogin;
import com.zb.sannuo.common.domain.UserRole;
import com.zb.sannuo.common.domain.vo.DrugstoreInfoVo;
import com.zb.sannuo.common.result.Result;
import com.zb.sannuo.common.result.ResultCode;
import com.zb.sannuo.pharmacy.service.DrugstoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/drugstore")
@Api(tags = "连锁药房管理模块",description = "对药房进行CRUD操作")
@CrossOrigin
public class DrugstoreController {

    @Resource
    private DrugstoreService drugstoreServiceImpl;

    @ApiOperation(value = "根据条件查询门店信息且分页操作",notes = "根据条件查询门店信息且分页操作",httpMethod = "GET")
    @RequestMapping(value = "findMD",method = RequestMethod.GET)
    public Result findMenDian(Integer page,Integer limit,String searchParams) throws Exception{

        ObjectMapper mapper = new ObjectMapper();
        DrugstoreInfoVo drugstoreInfoVo = null;
        if (searchParams != null && !"".equals(searchParams)){
            drugstoreInfoVo = mapper.readValue(searchParams, DrugstoreInfoVo.class);
        }
        System.out.println(drugstoreInfoVo);
        Page listPage = drugstoreServiceImpl.selectMDListPage(page, limit,drugstoreInfoVo);
        System.out.println(listPage.getTotal());

        return Result.ok(ResultCode.SUCCESS,listPage.getTotal(),listPage.getRecords());
    }

    @ApiOperation(value = "根据条件查询分店信息且分页操作",notes = "根据条件查询分店信息且分页操作",httpMethod = "GET")
    @RequestMapping(value = "findFD/{storeCodeZd}",method = RequestMethod.GET)
    public Result findFenDian(Integer page,Integer limit,String searchParams,@PathVariable("storeCodeZd") String storeCodeZd) throws Exception{

        ObjectMapper mapper = new ObjectMapper();
        DrugstoreInfoVo drugstoreInfoVo = new DrugstoreInfoVo();
        if(storeCodeZd!=null){
            drugstoreInfoVo.setStoreCodeZd(storeCodeZd);
            System.out.println("总店编码==="+storeCodeZd);
        }
        if (searchParams != null && !"".equals(searchParams)){
            drugstoreInfoVo = mapper.readValue(searchParams, DrugstoreInfoVo.class);
        }

        Page listPage = drugstoreServiceImpl.selectFDListPage(page, limit,drugstoreInfoVo);
        System.out.println(listPage.getTotal());

        return Result.ok(ResultCode.SUCCESS,listPage.getTotal(),listPage.getRecords());
    }

    @ApiOperation(value = "添加分店信息",notes = "添加分店信息",httpMethod = "POST")
    @RequestMapping(value = "addFd",method = RequestMethod.POST)
    public Result addSubStore(Drugstore drugstore, UserLogin userLogin, UserRole userRole) throws Exception{

        boolean b = drugstoreServiceImpl.addSubStore(drugstore,userLogin,userRole);
        System.out.println(b);
        return Result.ok(ResultCode.SUCCESS,b);

    }

}
